package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcBghzService;
import com.augurit.gzcgwsp.jzfqwczz.web.form.YwYnJzfqwczcJbxxForm;
import com.augurit.gzcgwsp.readAndParseXML.service.ICzzBghzService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 处置证 变更换证
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class CzzBghzServiceImpl implements ICzzBghzService{
	Logger logger = (Logger) LoggerFactory.getLogger(CzzBghzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;
	@Autowired
	private IYwYnJzfqwczcBghzService ywYnJzfqwczcBghzService;
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String czzBghzfilepath = localResource.getString("czzBghzfilepath");

	// 附件存放路径
	private final String czzBghzfujianpath = localResource.getString("czzBghzfujianpath");

	// 解析成功反馈xml存放路径
	private final String czzBghzfeedbackPath = localResource.getString("czzBghzfeedbackPath");

	// 解析异常xml存放路径
	private final String czzBghzexceptionPath = localResource.getString("czzBghzexceptionPath");

	// 解析后，xml备份
	private final String czzBghzbackPath = localResource.getString("czzBghzbackPath");

	private Map<String, String> czzBghzKeyMap;// xml内容的key

	private Map<String, String> czzBghzKeyMapHead;// xml头部的key

	private Map<String, Long> czzBghzfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initczzBghzMap() {
		// 初始化数据部分对应的map
		czzBghzKeyMap = new HashMap<String, String>();
		czzBghzKeyMap.put("approvalNo", "approvalNo");
		czzBghzKeyMap.put("businessCode", "businessCode");
		czzBghzKeyMap.put("YSL_NO", "YSL_NO");
		czzBghzKeyMap.put("sendTime", "sendTime");
		
		czzBghzKeyMap.put("SQDW", "SQDW");
		czzBghzKeyMap.put("QYJC", "QYJC");
		czzBghzKeyMap.put("QYPHONE", "QYPHONE");
		czzBghzKeyMap.put("ADDRESS", "ADDRESS");
		czzBghzKeyMap.put("POST", "POST");
		czzBghzKeyMap.put("CZ", "CZ");
		czzBghzKeyMap.put("FRXM", "FRXM");
		czzBghzKeyMap.put("FRPHONE", "FRPHONE");
		czzBghzKeyMap.put("YWLXR", "YWLXR");
		czzBghzKeyMap.put("YWLXRTEL", "YWLXRTEL");
		czzBghzKeyMap.put("ZYDM", "ZYDM");
		czzBghzKeyMap.put("GSZCDJH", "GSZCDJH");
		czzBghzKeyMap.put("GSZCDJYXQ", "GSZCDJYXQ");
		czzBghzKeyMap.put("QYDLYSZH", "QYDLYSZH");
		czzBghzKeyMap.put("QYDLYSZYXQ", "QYDLYSZYXQ");
		czzBghzKeyMap.put("ZZDJ", "ZZDJ");
		czzBghzKeyMap.put("CLS", "CLS");
		czzBghzKeyMap.put("SFYX", "SFYX");
		czzBghzKeyMap.put("CZZH", "CZZH");
		czzBghzKeyMap.put("ZZDJ", "ZZDJ");
		czzBghzKeyMap.put("FRBG", "FRBG");
		czzBghzKeyMap.put("BG_SQDW", "BG_SQDW");
		czzBghzKeyMap.put("BG_DBRXM", "BG_DBRXM");
		czzBghzKeyMap.put("BG_DBRDH", "BG_DBRDH");
		czzBghzKeyMap.put("BG_ADDRESS", "BG_ADDRESS");
		czzBghzKeyMap.put("BG_DJH", "BG_DJH");
		czzBghzKeyMap.put("BG_DJH_LIMIT", "BG_DJH_LIMIT");
		czzBghzKeyMap.put("BG_YSZH", "BG_YSZH");
		czzBghzKeyMap.put("BG_YSZH_LIMIT", "BG_YSZH_LIMIT");
		czzBghzKeyMap.put("BRANCH", "BRANCH");

		// 初始化头部对应的map
		czzBghzKeyMapHead = new HashMap<String, String>();
		czzBghzKeyMapHead.put("sn", "sn");
		czzBghzKeyMapHead.put("sender", "sender");
		// czzBghzKeyMapHead.put("receiver", "receiver");
		czzBghzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		czzBghzfujianMap = new HashMap<String, Long>();
		czzBghzfujianMap.put("FJ_01", 243l);
		czzBghzfujianMap.put("FJ_02", 244l);
		czzBghzfujianMap.put("FJ_03", 245l);
		czzBghzfujianMap.put("FJ_04", 246l);
		czzBghzfujianMap.put("FJ_05", 247l);
		czzBghzfujianMap.put("FJ_06", 248l);
		czzBghzfujianMap.put("FJ_07", 249l);
		czzBghzfujianMap.put("FJ_08", 250l);
		czzBghzfujianMap.put("FJ_09", 251l);
	}

	public void readXml(String projectPath) {
		String templeCode = "";
		if (czzBghzKeyMap == null)
			this.initczzBghzMap();
		File file = new File(czzBghzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(czzBghzKeyMap, filePath,czzBghzfeedbackPath, czzBghzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(czzBghzKeyMapHead,files[i], czzBghzfeedbackPath, czzBghzexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczcJbxxForm form = new YwYnJzfqwczcJbxxForm();
						for (String mapKey : czzBghzKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(czzBghzKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(czzBghzKeyMap.get("SQDW")));
							templeCode = parseXMLService.getMapValue(czzBghzKeyMap.get("BRANCH"));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(czzBghzKeyMap.get("SQDW")),templeCode);
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}
							
							form.setQyjc(parseXMLService.getMapValue(czzBghzKeyMap.get("QYJC")));
							form.setQyphone(parseXMLService.getMapValue(czzBghzKeyMap.get("QYPHONE")));
							form.setAddress(parseXMLService.getMapValue(czzBghzKeyMap.get("ADDRESS")));
							form.setPost(parseXMLService.getMapValue(czzBghzKeyMap.get("POST")));
							form.setCz(parseXMLService.getMapValue(czzBghzKeyMap.get("CZ")));
							form.setFrxm(parseXMLService.getMapValue(czzBghzKeyMap.get("FRXM")));
							form.setFrphone(parseXMLService.getMapValue(czzBghzKeyMap.get("FRPHONE")));
							form.setYwlxr(parseXMLService.getMapValue(czzBghzKeyMap.get("YWLXR")));
							form.setYwlxrtel(parseXMLService.getMapValue(czzBghzKeyMap.get("YWLXRTEL")));
							form.setZydm(parseXMLService.getMapValue(czzBghzKeyMap.get("ZYDM")));
							form.setGszcdjh(parseXMLService.getMapValue(czzBghzKeyMap.get("GSZCDJH")));
							String gszcdjyxqStr = parseXMLService.getMapValue(czzBghzKeyMap.get("GSZCDJYXQ"));
							Date gszcdjyxq = null;
							if (StringUtils.isNotEmpty(gszcdjyxqStr)) {
								gszcdjyxq = DateUtils.stringToDate(gszcdjyxqStr, DateUtils.YYYY_MM_DD);
								form.setGszcdjyxq(gszcdjyxq);
							}
							
							form.setQydlyszh(parseXMLService.getMapValue(czzBghzKeyMap.get("QYDLYSZH")));
							String qydlyszyxqStr = parseXMLService.getMapValue(czzBghzKeyMap.get("QYDLYSZYXQ"));
							Date qydlyszyxq = null;
							if (StringUtils.isNotEmpty(qydlyszyxqStr)) {
								qydlyszyxq = DateUtils.stringToDate(qydlyszyxqStr, DateUtils.YYYY_MM_DD);
								form.setQydlyszyxq(qydlyszyxq);
							}
							form.setZzdj(parseXMLService.getMapValue(czzBghzKeyMap.get("ZZDJ")));
							form.setCls(parseXMLService.getMapValue(czzBghzKeyMap.get("CLS")));
							form.setSfyx(parseXMLService.getMapValue(czzBghzKeyMap.get("SFYX")));
							form.setCzzh(parseXMLService.getMapValue(czzBghzKeyMap.get("CZZH")));
							form.setFrbg(parseXMLService.getMapValue(czzBghzKeyMap.get("FRBG")));
							czzBghzKeyMap.put("BG_YSZH", "BG_YSZH");
							czzBghzKeyMap.put("BG_YSZH_LIMIT", "BG_YSZH_LIMIT");
							form.setBgSqdw(parseXMLService.getMapValue(czzBghzKeyMap.get("BG_SQDW")));
							form.setBgDbrxm(parseXMLService.getMapValue(czzBghzKeyMap.get("BG_DBRXM")));
							form.setBgDbrdh(parseXMLService.getMapValue(czzBghzKeyMap.get("BG_DBRDH")));
							form.setBgAddress(parseXMLService.getMapValue(czzBghzKeyMap.get("BG_ADDRESS")));
							form.setBgDjh(parseXMLService.getMapValue(czzBghzKeyMap.get("BG_DJH")));
							String bgDjhLimitStr = parseXMLService.getMapValue(czzBghzKeyMap.get("BG_DJH_LIMIT"));
							Date bgDjhLimit = null;
							if (StringUtils.isNotEmpty(bgDjhLimitStr)) {
								bgDjhLimit = DateUtils.stringToDate(bgDjhLimitStr, DateUtils.YYYY_MM_DD);
								form.setBgDjhLimit(bgDjhLimit);
							}
							
							form.setBranch(templeCode);
							form.setApprovalNo(parseXMLService.getMapValue(czzBghzKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(czzBghzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(czzBghzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczcBghzService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									czzBghzKeyMapHead, czzBghzfeedbackPath, czzBghzexceptionPath, i);
						}
						//处理附件
						parseXMLService.handleFj(projectPath, fileName, czzBghzfujianpath, czzBghzfeedbackPath, czzBghzexceptionPath, czzBghzKeyMapHead,
								files, czzBghzfujianMap, i, form.getQyid(), form.getId(), templeCode);
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								czzBghzKeyMapHead, czzBghzfeedbackPath, czzBghzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, czzBghzbackPath + fileName,fileName);
				}
			}
		}
	}

}
